<?php

namespace Modules\Api\Service;

use HiCommon\Model\OrderCheckpoint;
use HiCommon\Model\OrderCheckpointVehicle;
use HiCommon\Repository\OrderCheckpointRefundRepository;
use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Service\OrderService;

class CheckpointOrderService
{
    /************************************************** 用户端订单及操作 **************************************************/

    /**
     * 用户删除订单
     *
     * @param $n_uid
     * @param $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_user_del_order($n_uid,$n_id){
        if ($n_uid <= 0){
            return hello_error('用户id不能为空');
        }
        if ($n_id <= 0){
            return hello_error('订单id不能为空');
        }

        //查询订单信息 只能删除自己的订单
        $arr_option = [
          'field'=>['id','user_delete','user_id']
        ];
        $result = OrderCheckpointRepository::base_find_one($n_id,$arr_option);
        if(is_null($result)){
            return hello_error('订单不存在');
        }
        $result = $result->toarray();
        if($result['user_delete'] == 1){
            return hello_error('订单已删除');
        }

        if($result['user_id'] != $n_uid){
            return hello_error('只能删除自己的订单');
        }

        //删除订单
        $arr_data = [
          'id'=>$n_id,
          'user_delete'=>1,
          'user_delete_time'=>time_to_ymdhis(),
        ];
        $res = OrderCheckpointRepository::base_edit_one($arr_data);
        if($res === false){
            return hello_error('删除失败');
        }
        return  hello_success('删除成功');

    }

    /**
     * 转换搜索条件为真实搜索字段
     *
     * @param $s_search_type
     * @param $arr_search_where
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function transfer_search_type($s_search_type,&$arr_search_where){
        switch ($s_search_type)
        {
            case 'all':
                $arr_search_where = [];
                break;
            case 'appointment_success':
                $arr_search_where = [
                  'pay_state'=>2,
                  'appointment_state'=>2,
//                  'complete_state'=>-1,
                ];
                break;
            case 'appointment_ing':
                $arr_search_where = [
                  'pay_state'=>1,
                  'appointment_state'=>1,
                ];
                break;
            case 'be_canceled':
                $arr_search_where = [
                  'appointment_state'=>3,
                ];
                break;
            case 'waiting_apprise':
                $arr_search_where = [
                  'appraise_state'=>1
                ];
                break;
            default:
                $arr_search_where = [];

        }

    }

    /**
     * 根据用户id获取年检预约订单列表
     *
     * @param $n_uid
     * @param $arr_search_where
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_order_list_by_uid($n_uid,$arr_search_where){
        $arr_search_where['user_id'] = $n_uid;
        $arr_search_where['user_delete'] = 2;
        $arr_option = [
          'where'=>$arr_search_where,
          'field'=>['id','order_sn','main_type','appointment_date','appointment_times','shop_id','user_company_id','price','pay_state','refund_state','appointment_state','complete_state','appraise_state','user_delete','created_at'],
          'with'=>['shop','user_company','check_vehicle']
        ];

        $res = OrderCheckpointRepository::base_api_get_list($arr_option)->toarray();
        $data = list_page_data_for_api($res);
        return  hello_success('成功',$data);
    }

//    /**
//     * 处理订单状态
//     *
//     * @param $arr_input
//     *
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function api_deal_for_state(&$arr_input){
//        $n_pay_state = intval($arr_input['pay_state']);
//        $n_refund_state = intval($arr_input['refund_state']);
//        $n_appointment_state = intval($arr_input['appointment_state']);
//        $n_complete_state = intval($arr_input['complete_state']);
//        $n_appraise_state = intval($arr_input['appraise_state']);
//
//        switch ($n_pay_state)
//        {
//            //支付状态：1待支付 2已支付
//            case 2:
//                $arr_input['pay_state_name'] = '已支付';
//                break;
//            case 1:
//                $arr_input['pay_state_name'] = '未支付';
//                break;
//            default:
//                $arr_input['pay_state_name'] = '';
//        }
//
//        switch ($n_refund_state)
//        {
//            //退款状态：0默认 1未退款 2部分退款 3全部退款-整单退款 4全部退款-逐个累计
//            case 1:
//                $arr_input['refund_state_name'] = '未退款';
//                break;
//            case 2:
//                $arr_input['refund_state_name'] = '部分退款';
//                break;
//            case 3:
//                $arr_input['refund_state_name'] = '全部退款';
//                break;
//            case 4:
//                $arr_input['refund_state_name'] = '全部退款';
//                break;
//            default:
//                $arr_input['refund_state_name'] = '';
//        }
//
//        switch ($n_appointment_state)
//        {
//            //预约状态：1预约中 2预约成功 3预约失败 [9预约取消]
//            case 1:
//                $arr_input['appointment_state_name'] = '预约中';
//                break;
//            case 2:
//                $arr_input['appointment_state_name'] = '预约成功';
//                break;
//            case 3:
//                $arr_input['appointment_state_name'] = '预约失败';
//                break;
//            default:
//                $arr_input['appointment_state_name'] = '';
//        }
//
//        switch ($n_complete_state)
//        {
//            //订单完成状态：0默认 1待完成-等待用户确认完成 2已完成-用户已确认完成 3已结束-订单全部退款已终止 -1待检测
//            case -1:
//                $arr_input['complete_state_name'] = '待检测';
//                break;
//            case 1:
//                $arr_input['complete_state_name'] = '待完成';
//                break;
//            case 2:
//                $arr_input['complete_state_name'] = '已完成';
//                break;
//            case 3:
//                $arr_input['complete_state_name'] = '已结束';
//                break;
//            default:
//                $arr_input['complete_state_name'] = '';
//        }
//
//        switch ($n_appraise_state)
//        {
//            //评价状态：0默认 1待评价 2已评价
//
//            case 1:
//                $arr_input['appraise_state_name'] = '待评价';
//                break;
//            case 2:
//                $arr_input['appraise_state_name'] = '已评价';
//                break;
//            default:
//                $arr_input['appraise_state_name'] = '';
//        }
//
//
//
//        // 10-待支付 21-预约失败（已取消） 22-预约成功 30-待确认完成 31-已完成-待评价 32-已完成-已评价 -1状态异常
//        $n_goto_pay = 2;//立即支付 1是 2否
//        $n_cancel_order = 2;//取消预约 1是 2否
//        $n_goto_apprise = 2;//立即评价 1是 2否
//        $n_del_order = 2;//删除订单 1是 2否
//        $n_waiting_confirm = 2;//确认完成 1是 2否
//        if($n_complete_state == 1){
//            $n_waiting_confirm = 1;
//        }
//        if($n_pay_state == 1 && $n_appointment_state == 1){
//            $n_order_state = 10;
//            $s_order_state_name = '待支付';
//            $n_goto_pay = 1;
//            $n_cancel_order = 1;
//        }
//        elseif ($n_appointment_state == 3){
//            $n_order_state = 21;
//            $s_order_state_name = '预约失败';
//        }
//        elseif ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == 1){
//            $n_order_state = 30;
//            $s_order_state_name = '待确认完成';
//        }
//        elseif ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == -1){
//            $n_order_state = 22;
//            $s_order_state_name = '预约成功';
//        }
//        elseif ($n_complete_state == 2){
//            if ($n_appraise_state == 1){
//                $n_goto_apprise = 1;
//                $n_order_state = 31;
//                $s_order_state_name = '待评价';
//            }
//            elseif($n_appraise_state == 2){
//                $n_order_state = 32;
//                $s_order_state_name = '已评价';
//            }
//            else{
//                $n_order_state = -1;
//                $s_order_state_name = '状态异常';
//            }
//        }
//        else{
//            $n_order_state = -1;
//            $s_order_state_name = '状态异常';
//        }
//
//
//        if ($n_appointment_state == 3 || in_array($n_complete_state,[2,3])){
//            $n_del_order = 1;
//        }
//
//        $arr_input['order_state'] = $n_order_state;
//        $arr_input['order_state_name'] = $s_order_state_name;
//        $arr_input['user_waiting_operate'] = [
//          'goto_pay'=>$n_goto_pay,
//          'cancel_order'=>$n_cancel_order,
//          'goto_apprise'=>$n_goto_apprise,
//          'del_order'=>$n_del_order,
//          'waiting_confirm'=>$n_waiting_confirm
//        ];
////        $arr_input['shop_waiting_operate'] = $n_order_state;
//
//    }

    /**
     * 处理列表数据
     *
     * @param $arr_data
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_deal_for_list(&$arr_data){
        foreach ($arr_data as &$value)
        {
            $n_is_call_helpdriving = 2;
            if ($value['main_type'] == 1) {
                $n_is_call_helpdriving = $value['check_vehicle'][0]['is_call_helpdriving'];
            }

            $arr_shop         = $value['shop'];
            $arr_shop['logo'] = $arr_shop['logo']['full_src'];

            $arr_user_company                 = $value['user_company'];
            $arr_user_company['company_logo'] = $arr_user_company['company_logo']['full_src'];

//            $arr_check_vehicle                 = $value['check_vehicle'];

            $value                            = [
              'id'                  => $value['id'],
              'order_sn'            => $value['order_sn'],
              'main_type'           => $value['main_type'],
              'is_call_helpdriving' => $n_is_call_helpdriving,
              'appointment_date'    => $value['appointment_date'],
              'appointment_times'   => $value['appointment_times'],
              'price'               => $value['price'],
              'pay_state'           => $value['pay_state'],
              'refund_state'        => $value['refund_state'],
              'appointment_state'   => $value['appointment_state'],
              'complete_state'      => $value['complete_state'],
              'appraise_state'      => $value['appraise_state'],
              'created_at'          => $value['created_at'],
              //              'order_state'=>$value['extend']['order_state']['id'],
              'shop'                => $arr_shop,
              'user_company'        => $arr_user_company,
//              'check_vehicle'        => $arr_check_vehicle,
            ];

            //处理主订单状态
//            CheckpointOrderService::api_deal_for_state($value);
            OrderCheckpoint::deal_for_state($value);


        }

    }

    /**
     * 用户年检预约单列表
     *
     * @param $n_uid
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_user_order_list($n_uid,$arr_input){

        //转换搜索条件
        $s_search_type = $arr_input['search_type'];
        CheckpointOrderService::transfer_search_type($s_search_type,$arr_search_where);

        //查询数据
        if (isset($arr_input['main_type'])){
            $arr_search_where['main_type'] = $arr_input['main_type'];
        }

        $result = CheckpointOrderService::api_order_list_by_uid($n_uid,$arr_search_where);

        //处理列表数据
       if($result['state'] == 0){
           CheckpointOrderService::api_deal_for_list($result['data']['data']);
       }

       return $result;

    }

//    public function api_deal_single_for_vehicle_state(&$data,&$n_can_apply_refund){
//        $n_apply_refund_state = intval($data['apply_refund_state']);
//        $n_check_state = intval($data['check_state']);
//        $s_msg = '';
//        $n_state = -1;
//        $n_apply_refund = 2;
//        //-1状态异常 10待检测 20检测成功-合格 30检测成功-不合格 51退款中 52待退款 53已退款
//        if ($n_check_state == 1){
//            //1待检测
//            $s_msg = '待检测';
//            $s_msg_full = '待检测';
//            $n_state = 10;
//            $n_apply_refund = 1;
//        }
//        elseif($n_check_state == 2){
//            //2检测成功-合格
//            $s_msg = '车辆合格';
//            $s_msg_full = '检测成功-合格';
//            $n_state = 20;
//        }
//        elseif($n_check_state == 3){
//            //3检测成功-不合格
//            $s_msg_full = '检测成功-不合格';
//            $s_msg = '车辆不合格';
//            $n_state = 30;
//        }
//        elseif($n_check_state == 9){
//            //9检测失败（不符合检测条件-退款）
//            if ($n_apply_refund_state == 2){
//                //待退款
//                $s_msg = '待退款';
//                $s_msg_full = '不符合检测条件-待退款';
//                $n_state = 52;
//            }
//            elseif($n_apply_refund_state == 3){
//                //已退款
//                $s_msg = '已退款';
//                $s_msg_full = '不符合检测条件-已退款';
//                $n_state = 53;
//            }
//            else{
//                //状态异常90
//                $s_msg = '状态异常';
//                $s_msg_full = '不符合检测条件-状态异常';
//            }
//        }
//        elseif($n_check_state == 10){
//            //10检测失败（用户申请退款-退款）
//
//            if ($n_apply_refund_state == 1){
//                //申请中
//                $s_msg = '退款申请中';
//                $s_msg_full = '用户申请退款-申请中';
//                $n_state = 51;
//            }
//            elseif ($n_apply_refund_state == 2){
//                //待退款
//                $s_msg = '待退款';
//                $s_msg_full = '用户申请退款-待退款';
//                $n_state = 52;
//            }
//            elseif($n_apply_refund_state == 3){
//                //已退款
//                $s_msg = '已退款';
//                $s_msg_full = '用户申请退款-已退款';
//                $n_state = 53;
//            }
//            elseif($n_apply_refund_state == 9){
//                //待检测
//                $s_msg = '退款申请中';
//                $s_msg_full = '用户申请退款-待检测';
//                $n_state = 10;
//                $n_apply_refund = 1;
//            }
//            else{
//                //状态异常100
//                $s_msg = '状态异常';
//                $s_msg_full = '用户申请退款-状态异常';
//            }
//        }
//        else{
//            //状态异常0
//            $s_msg = '状态异常';
//            $s_msg_full = '状态异常';
//        }
//        $data['order_state'] = $n_state;
//        $data['order_state_name'] = $s_msg;
//        $data['order_state_full_name'] = $s_msg_full;
//        $data['user_waiting_operate'] = [
//          'apply_refund'=>$n_apply_refund,
//        ];
//
//        if($n_apply_refund == 1){
//            $n_can_apply_refund = 1;
//        }
//    }

    /**
     * 处理车辆子订单状态
     *
     * @param $arr_input
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_deal_for_vehicle_state(&$arr_input,&$n_can_apply_refund){

        $n_can_apply_refund = 2;//是否可申请退款 1是2否
        foreach ($arr_input as &$value)
        {
            $value['user_vehicle']['driving_permit_img'] = $value['user_vehicle']['driving_permit_img']['full_src'];
//            CheckpointOrderService::api_deal_single_for_vehicle_state($value,$n_can_apply_refund);
            OrderCheckpointVehicle::deal_for_state($value,$n_can_apply_refund);

        }



        //        $arr_input['shop_waiting_operate'] = $n_order_state;

    }

    public function api_deal_for_detail(&$arr_input){
        $arr_shop = $arr_input['shop'];
        if(!is_null($arr_shop)){
            $arr_shop['logo'] = $arr_shop['logo']['full_src'];
        }

        $arr_user_company = $arr_input['user_company'];
        if(!is_null($arr_user_company)){
            $arr_user_company['company_logo'] = $arr_user_company['company_logo']['full_src'];
        }

        $arr_shop_checkpoint_apprise = $arr_input['shop_checkpoint_apprise'];
        $arr_order_apprise = null;
        if(!is_null($arr_shop_checkpoint_apprise)){
            $arr_order_apprise = [
              'id'=>$arr_shop_checkpoint_apprise['id'],
              'score'=>$arr_shop_checkpoint_apprise['score'],
              'content'=>$arr_shop_checkpoint_apprise['content'],
            ];
        }

        foreach ($arr_input['check_vehicle'] as &$value)
        {
            $value['user_vehicle']['vehicle_model'] = $value['user_vehicle']['simple_vehicle_model'];
            unset($value['user_vehicle']['simple_vehicle_model']);
        }

        $arr_input = [
          'id'=>$arr_input['id'],
          'order_sn'=>$arr_input['order_sn'],
          'main_type'=>$arr_input['main_type'],
          'appointment_date'=>$arr_input['appointment_date'],
          'appointment_times'=>$arr_input['appointment_times'],
          'appointment_time_type'=>$arr_input['appointment_time_type'],
          'appointment_num'=>$arr_input['appointment_num'],
          'price'=>$arr_input['price'],
          'pay_price'=>$arr_input['pay_price'],
//          'refund_price'=>$arr_input['refund_price'],
//          'remarks'=>$arr_input['remarks'],
//          'deal_remarks'=>$arr_input['deal_remarks'],
          'payment_method'=>$arr_input['payment_method'],
          'pay_state'=>$arr_input['pay_state'],
          'refund_state'=>$arr_input['refund_state'],
          'appointment_state'=>$arr_input['appointment_state'],
          'complete_state'=>$arr_input['complete_state'],
          'appraise_state'=>$arr_input['appraise_state'],
//          'order_state'=>$arr_input['extend']['order_state']['id'],
          'is_invoice'=>$arr_input['is_invoice'],
          'pay_time'=>$arr_input['pay_time'],
//          'refund_time'=>$arr_input['refund_time'],
          'created_at'=>$arr_input['created_at'],
//          'updated_at'=>$arr_input['updated_at'],
//          'deleted_at'=>$arr_input['deleted_at'],
          //              'shop_checkpoint_apprise'=>$arr_input['shop_checkpoint_apprise'],
          //              'wechat_refund'=>$arr_input['wechat_refund'],

          'shop'=>$arr_shop,
          'user_company'=>$arr_user_company,
          'order_apprise'=>$arr_order_apprise,
          'check_vehicle'=>$arr_input['check_vehicle'],

        ];
        //处理主单状态
//        CheckpointOrderService::api_deal_for_state($arr_input);
        OrderCheckpoint::deal_for_state($arr_input);

        //处理车辆子单状态
        CheckpointOrderService::api_deal_for_vehicle_state($arr_input['check_vehicle'],$n_can_apply_refund);

        $arr_input['user_waiting_operate']['apply_refund'] = $n_can_apply_refund;

    }

    /**
     * 用户年检预约单详情
     *
     * @param int $n_uid
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_user_order_detail($n_uid = 0,$arr_input = []){
        $n_id = intval($arr_input['id']);
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        $result = OrderCheckpointRepository::api_user_get_detail($n_id);
        if(is_null($result)){
            return hello_error('数据不存在');
        }
        $result = $result->toarray();
        $n_user_id = $result['user_id'];

        if($n_user_id != $n_uid){
            return hello_error('非本人订单');
        }
        CheckpointOrderService::api_deal_for_detail($result);

        //求两个已知经纬度之间的距离
        $start_longitude = $arr_input['longitude'];
        $start_latitude = $arr_input['latitude'];
        $end_longitude = $result['shop']['longitude'];
        $end_latitude = $result['shop']['latitude'];
        $arr_distance = null;
        if(!(empty($start_longitude) || empty($start_latitude) || empty($end_longitude) || empty($end_latitude))){
            $arr_distance = location_distance($start_longitude,$start_latitude,$end_longitude,$end_latitude);
        }
        $result['distance'] = $arr_distance;
        return  hello_success('成功',$result);
    }

    /*处理[用户]售后列表*/
    public function deal_with_aftersale_list(&$arr_input = []){

        //车辆订单信息
        $arr_vehicle = $arr_input['vehicle'];
        //            $arr_user_vehicle = $arr_input['vehicle']['user_vehicle'];
        $arr_simple_vehicle_model = $arr_input['vehicle']['user_vehicle']['simple_vehicle_model'];
        $arr_vehicle_data = [
          'id'=>$arr_vehicle['id'],
          'order_sn'=>$arr_vehicle['order_sn'],
          'plate_no'=>$arr_vehicle['plate_no'],
          'owner'=>$arr_vehicle['owner'],
          'price'=>$arr_vehicle['price'],
          'name'=>$arr_simple_vehicle_model['name'],
          'one_name'=>$arr_simple_vehicle_model['one_name'],
          'two_name'=>$arr_simple_vehicle_model['two_name'],
          'three_name'=>$arr_simple_vehicle_model['three_name'],
          'logo'=>$arr_simple_vehicle_model['logo'],
        ];
        $arr_input['vehicle'] = $arr_vehicle_data;

        //检测机构信息
        $arr_shop = $arr_input['order_for_user']['shop'];
        if(!is_null($arr_shop)){
            $arr_shop['logo'] = $arr_shop['logo']['full_src'];
        }
        $arr_input['shop'] = $arr_shop;
        
        
        //故障原因
        $arr_fail_type = $arr_input['fail_type'];
        $arr_input['fail_type'] = $arr_fail_type['fail_type'];

        //用户企业信息
        $arr_user_company = $arr_input['order_for_user']['user_company'];
        if(!is_null($arr_user_company)){
            $arr_user_company['company_logo'] = $arr_user_company['company_logo']['full_src'];
        }
        $arr_input['user_company'] = $arr_user_company;

        //主订单信息
        $arr_order = [
          'id'=>$arr_input['order_for_user']['id'],
          'main_type'=>$arr_input['order_for_user']['main_type'],
          'phone'=>$arr_input['order_for_user']['phone'],
          'realname'=>$arr_input['order_for_user']['realname'],
          'appointment_date'=>$arr_input['order_for_user']['appointment_date'],
          'appointment_times'=>$arr_input['order_for_user']['appointment_times'],
          'appointment_time_type'=>$arr_input['order_for_user']['appointment_time_type'],
        ];

        unset($arr_input['order_for_user']);
        $arr_input['order'] = $arr_order;
    }

    /*处理[商户]售后列表*/
    public function deal_with_aftersale_list_for_shop(&$arr_input = []){


        //车辆订单信息
        $arr_vehicle = $arr_input['vehicle'];

//        CheckpointOrderService::api_deal_single_for_vehicle_state($arr_vehicle,$n_can_apply_refund);
        OrderCheckpointVehicle::deal_for_state($arr_vehicle,$n_can_apply_refund);

        //            $arr_user_vehicle = $arr_input['vehicle']['user_vehicle'];
        $arr_simple_vehicle_model = $arr_input['vehicle']['user_vehicle']['simple_vehicle_model'];
        $arr_vehicle_data = [
          'id'=>$arr_vehicle['id'],
          'is_call_helpdriving'=>$arr_vehicle['is_call_helpdriving'],
          'order_sn'=>$arr_vehicle['order_sn'],
          'plate_no'=>$arr_vehicle['plate_no'],
          'owner'=>$arr_vehicle['owner'],
          'price'=>$arr_vehicle['price'],
          'name'=>$arr_simple_vehicle_model['name'],
          'one_name'=>$arr_simple_vehicle_model['one_name'],
          'two_name'=>$arr_simple_vehicle_model['two_name'],
          'three_name'=>$arr_simple_vehicle_model['three_name'],
          'logo'=>$arr_simple_vehicle_model['logo'],
          'order_state'=>$arr_vehicle['order_state'],
          'order_state_name'=>$arr_vehicle['order_state_name'],
          'order_state_full_name'=>$arr_vehicle['order_state_full_name'],
          'user_waiting_operate'=>$arr_vehicle['user_waiting_operate'],
          'shop_waiting_operate'=>$arr_vehicle['shop_waiting_operate'],
        ];
        $arr_input['vehicle'] = $arr_vehicle_data;
        $arr_order_shop = $arr_input['order_for_shop'];
        unset($arr_input['order_for_shop']);

        //检测机构信息
        $arr_user = $arr_order_shop['user'];
        if(!is_null($arr_user)){
            $arr_user['head_img'] = $arr_user['head_img']['full_src'];
        }
        $arr_input['user'] = $arr_user;

        //故障原因
        $arr_fail_type = $arr_input['fail_type'];
        $arr_input['fail_type'] = $arr_fail_type['fail_type'];

        //用户企业信息
        $arr_user_company = $arr_order_shop['user_company'];
        if(!is_null($arr_user_company)){
            $arr_user_company['company_logo'] = $arr_user_company['company_logo']['full_src'];
        }
        $arr_input['user_company'] = $arr_user_company;

        //主订单信息
        $arr_order = [
          'id'=>$arr_order_shop['id'],
          'main_type'=>$arr_order_shop['main_type'],
          'phone'=>$arr_order_shop['phone'],
          'realname'=>$arr_order_shop['realname'],
          'appointment_date'=>$arr_order_shop['appointment_date'],
          'appointment_times'=>$arr_order_shop['appointment_times'],
          'appointment_time_type'=>$arr_order_shop['appointment_time_type'],
        ];

        $arr_input['order'] = $arr_order;
    }

    /*处理[商户]售后详情*/
    public function deal_with_aftersale_detail_for_shop(&$arr_input = []){

        //车辆订单信息
        $arr_vehicle = $arr_input['vehicle_detail'];
        unset($arr_input['vehicle_detail']);

        //处理车辆子单状态
//        CheckpointOrderService::api_deal_single_for_vehicle_state($arr_vehicle,$n_can_apply_refund);
        OrderCheckpointVehicle::deal_for_state($arr_vehicle['vehicle_detail'],$n_can_apply_refund);

        //            $arr_user_vehicle = $arr_input['vehicle']['user_vehicle'];
        $arr_simple_vehicle_model = $arr_vehicle['user_vehicle']['simple_vehicle_model'];
        unset($arr_vehicle['user_vehicle']);


        $arr_vehicle_data = $arr_vehicle;
        $arr_vehicle_data['name'] = $arr_simple_vehicle_model['name'];
        $arr_vehicle_data['one_name'] = $arr_simple_vehicle_model['one_name'];
        $arr_vehicle_data['two_name'] = $arr_simple_vehicle_model['two_name'];
        $arr_vehicle_data['three_name'] = $arr_simple_vehicle_model['three_name'];
        $arr_vehicle_data['logo'] = $arr_simple_vehicle_model['logo'];
        $arr_input['vehicle'] = $arr_vehicle_data;

        $arr_order_shop = $arr_input['order_for_shop'];
        unset($arr_input['order_for_shop']);

        //检测机构信息
        $arr_user = $arr_order_shop['user'];
        if(!is_null($arr_user)){
            $arr_user['head_img'] = $arr_user['head_img']['full_src'];
        }
        $arr_input['user'] = $arr_user;

        //故障原因
        $arr_fail_type = $arr_input['fail_type'];
        $arr_input['fail_type'] = $arr_fail_type['fail_type'];

        //用户企业信息
        $arr_user_company = $arr_order_shop['user_company'];
        if(!is_null($arr_user_company)){
            $arr_user_company['company_logo'] = $arr_user_company['company_logo']['full_src'];
        }
        $arr_input['user_company'] = $arr_user_company;

        //主订单信息
        $s_appointment_times = $arr_order_shop['appointment_times'];
        $s_true_appointment_times = str_replace(' ','',$s_appointment_times);
        $arr_order = [
          'id'=>$arr_order_shop['id'],
          'main_type'=>$arr_order_shop['main_type'],
          'phone'=>$arr_order_shop['phone'],
          'realname'=>$arr_order_shop['realname'],
          'appointment_date'=>$arr_order_shop['appointment_date'],
          'appointment_times'=>$s_true_appointment_times,
          'appointment_time_type'=>$arr_order_shop['appointment_time_type'],
          'appoint_time'=>$arr_order_shop['appointment_date'].' '.$arr_order_shop['appointment_times'],
          'created_at'=>$arr_order_shop['created_at'],
          'pay_time'=>$arr_order_shop['pay_time'],
        ];

        $arr_input['order'] = $arr_order;
    }


    /**
     * 年检预约单-售后列表-[用户]
     *
     * @param int $n_uid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_aftersale_list($n_uid = 0){
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        $result = OrderCheckpointRefundRepository::get_user_aftersale_list($n_uid)->toarray();
        foreach ($result['data'] as &$value)
        {
            CheckpointOrderService::deal_with_aftersale_list($value);
        }

        $result = list_page_data_for_api($result);
        return  hello_success('成功',$result);
    }

    /**
     * 年检预约单-售后列表-[用户]
     *
     * @param int $n_id
     * @param int $n_uid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_aftersale_detail($n_id = 0,$n_uid = 0){
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        $result = OrderCheckpointRefundRepository::get_user_aftersale_detail($n_id);
        if(is_null($result)){
            return hello_error('数据不存在');
        }
        $result = $result->toarray();
        CheckpointOrderService::deal_with_aftersale_list($result);
        foreach ($result['data'] as &$value)
        {
            CheckpointOrderService::deal_with_aftersale_list_for_user($value);
        }
        return  hello_success('成功',$result);
    }


    /**
     * 年检预约单-售后列表-商户
     *
     * @param int $n_shop_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_aftersale_list_by_shopid($n_shop_id = 0){
        if($n_shop_id <= 0){
            return hello_error('商户id不能为空');
        }

        $result = OrderCheckpointRefundRepository::get_shop_aftersale_list($n_shop_id)->toarray();
        foreach ($result['data'] as &$value)
        {
            CheckpointOrderService::deal_with_aftersale_list_for_shop($value);
        }

        $result = list_page_data_for_api($result);
        return  hello_success('成功',$result);
    }

    public function api_aftersale_detail_for_shop($n_id = 0){
        if($n_id <= 0){
            return hello_error('售后订单id不能为空');
        }
        $result = OrderCheckpointRefundRepository::get_shop_aftersale_detail($n_id);
        if(is_null($result)){
            return hello_error('数据不存在');
        }
        $result = $result->toarray();

        CheckpointOrderService::deal_with_aftersale_detail_for_shop($result);

        return  hello_success('成功',$result);

    }





    /**
     * 预约单评价成功回调
     *
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function notify_apprise_order($n_id = 0){
        if($n_id <= 0){
            return hello_error('订单id不能为空');
        }
        $arr_data = [
          'id'=>$n_id,
          'appraise_state'=>2
        ];
        $result = OrderCheckpointRepository::base_edit_one($arr_data);
        if( $result === false ){
            return hello_error('评价失败',[],'主单评价状态修改失败');
        }
        return  hello_success('评价成功');
    }


    /************************************************** 商户端订单及操作 **************************************************/

    /**
     * 商户预约订单列表-待处理列表
     *
     * @param int   $n_shop_id
     * @param array $arr_search
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_waiting_order_list_by_shopid($n_shop_id = 0,$arr_search = []){
        $arr_where['shop_id'] = $n_shop_id;
        $arr_where['pay_state'] = 2;
        $arr_where['appointment_state'] = 2;
        $arr_where['complete_state'] = -1;
        $arr_option['with'] = ['check_vehicle.user_vehicle','shop','user','user_company'];
        $res = OrderCheckpointRepository::get_list_by_where($arr_where,$arr_option,$arr_search)->toarray();
        $data = list_page_data_for_api($res);
        foreach ($data['data'] as &$value)
        {
            unset($value['extend']);
            $o_check_vehicle = collect($value['check_vehicle']);
            $value['vehicle_waiting_num'] = intval($o_check_vehicle->whereIn('check_state',[0,1])->count());
            $value['vehicle_complete_num'] = intval($o_check_vehicle->where('check_state',2)->count());
            $value['vehicle_fail_num'] = intval($o_check_vehicle->where('check_state',9)->count());

//            CheckpointOrderService::api_deal_for_state($value);
            OrderCheckpoint::deal_for_state($value);

            //处理车辆子单状态
            CheckpointOrderService::api_deal_for_vehicle_state($value['check_vehicle'],$n_can_apply_refund);

        }


        return  hello_success('成功',$data);
    }

    public function api_complete_order_list_by_shopid($n_shop_id = 0,$arr_search = []){
        $arr_where['shop_id'] = $n_shop_id;
        //        $arr_where['check_state'] = 2;
        $arr_option['with'] = ['check_vehicle.user_vehicle','shop','user','user_company'];

        $res = OrderCheckpointRepository::get_list_by_where($arr_where,$arr_option,$arr_search)->toarray();
        $data = list_page_data_for_api($res);

        foreach ($data['data'] as &$item)
        {
            //处理主订单状态
//            CheckpointOrderService::api_deal_for_state($item);
            OrderCheckpoint::deal_for_state($item);

            //车辆子单处理类型归类
            $o_check_vehicle = collect($item['check_vehicle']);

            $item['vehicle_waiting_num'] = intval($o_check_vehicle->whereIn('check_state',[0,1])->count());
            $item['vehicle_complete_num'] = intval($o_check_vehicle->where('check_state',2)->count());
            $item['vehicle_fail_num'] = intval($o_check_vehicle->where('check_state',9)->count());

            //处理车辆子单状态
            CheckpointOrderService::api_deal_for_vehicle_state($item['check_vehicle'],$n_can_apply_refund);
        }


        return  hello_success('成功',$data);
    }
}